#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "uart.h"
#include "config.h"
#include <stdlib.h>
#include <ctype.h>

//*****************************************************************************
//	Function used to take a digit from UART
// Take character from  the UART and stores it into a array of characters. 
//If  the character is not a digit it  sends a message to the user to enter 
//another digit. The receiving stops when the user pres "Enter". The array 
// is convert to an unsigned char number.  The number is checked with the
// max number and if it is lower the function returns it. If not the user 
//has to enter another number. 
//*****************************************************************************
unsigned char ChoiseNum(unsigned char max){
	char buf[2];
	unsigned char choise;
	while(1){
			if (UARTCharsAvail(UART0_BASE)){
				buf[0] = (char) UARTCharGetNonBlocking(UART0_BASE);
				UARTCharPutNonBlocking(UART0_BASE, buf[0]);
				buf[1]='\0';
				choise = atoi(buf);
				if((choise > max)||(choise == 0)){
					UARTSend((unsigned char *)"\n\rWrong choise! Enter a new digit:\n\r",37);
					continue;
				}
				break;
				}
			}
	return choise;
}
//*****************************************************************************
//	Function used to take a number from UART.
//Take characters from  the UART and store them into a array of characters.
// If  some of  the characters is not a digit it stop receiving characters
// and sends a message to the user to enter another number. The receiving
// stops when the user pres "Enter". The array  is convert to an unsigned
// long number.  The number is checked with the max number and if it is lower 
//the function returns it. If not the user has to enter another number.
//*****************************************************************************
unsigned long Choice(unsigned long max)
{
	short datapos=0;
	char c, data[10]="";
	unsigned long choise;
	while(1){
		if (!(UARTCharsAvail(UART0_BASE)))continue;
			c = (char) UARTCharGetNonBlocking(UART0_BASE);
		UARTCharPutNonBlocking(UART0_BASE, c);
		if (c != 0x0D) {
			if(!isdigit(c)){
				UARTSend((unsigned char *)"\n\rWrong choise! Enter a new nomber:\n\r",37);
				continue;
			}
			data[datapos++] = c;
		}
		else{
			data[datapos] = '\0';
			UARTCharPutNonBlocking(UART0_BASE, '\n');
			datapos = 0;
			choise=atoi(data);  
			if(choise >= max){
				UARTSend((unsigned char *)"\n\rWrong choise! Enter a new nomber:\n\r",37);
				continue;
			}
			break;
		}
    }
	return choise;
}

//*****************************************************************************
//	Function used to send a string trough the UART to the PC.
//Parameters:
//pucBuffer is a pointer to the first element of the string.
//ulCount is the count of the string.
//
//It receives a  character  and write it to the UART while 
//there is enough space in the FIFO.
//*****************************************************************************

void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{	//
    // Loop while there are more characters to send.
	//
    while(ulCount--)
    {
	     while(!UARTSpaceAvail(UART0_BASE));
		//
        // Write the next character to the UART.
		//
        UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    }
}






